package ru.tulupov.toyfactory.pattern;

public abstract class Producer<T> extends Thread {
	private Storage<T> storage;

	public Producer(Storage<T> storage) {
		super("Producer");
		this.storage = storage;
	}

	@Override
	public void run() {
		onStart();
		while (!isInterrupted()) {
			try {
				T element = onCreate();
				storage.add(element);
			} catch (InterruptedException e) {
				break;
			}
		}
		onStop();
	}

	protected abstract T onCreate();

	protected void onStop() {

	}

	protected void onStart() {

	}

}
